package com.e3.sysmg.security.client;

import java.util.List;

import com.e3.sysmg.security.model.RoleModel;
import com.e3.web.core.client.ListenerManager;
import com.e3.web.core.client.QueryListener;
import com.e3.web.widget.client.MyDialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;

public class UserRoleQueryDialog extends MyDialog {

	private RoleUI roleUI;
	
	private UserMessages MESSAGES = UserMessages.INSTANCE;
	
	private ListenerManager<RoleModel> listenerManager = new ListenerManager<RoleModel>();
	
	@Override
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		setLayout(new FitLayout());
		setSize(600, 450);
		add(getRoleUI());
	}

	public RoleUI getRoleUI() {
		if(roleUI == null){
			roleUI = new RoleUI(){
				@Override
				public void onGridRowDoubClicked(RoleModel role) {
					onOk();
				}
			};
		}
		return roleUI;
	}
	
	public void addQueryListener(QueryListener<RoleModel> listener){
		listenerManager.addQueryListener(listener);
	}

	@Override
	protected void onOk() {
		List<RoleModel> roles = getRoleUI().getRoleGridPanel().getSelectedRoles();
		if(roles == null||roles.isEmpty()){
			MessageBox.alert(MESSAGES.msgbox_alert_title(),MESSAGES.msg_alert_choose_role(),null);
			return;
		}
		listenerManager.notifyQuerySuccess(null,roles);
		setVisible(false);
	}

}
